package com.example.myyiyanmengjava;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.example.mvplibrary.ui.activity.BaseActivity;
import com.example.myyiyanmengjava.bean.LoginBean;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.ResourceSubscriber;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class HomeActivity extends BaseActivity {
    private static String TAG = "朱勇闯";
    private TextView mTvTitleHome;
    private TextView mTvSizeHome;
    private TextView back;
    private ConstraintLayout con_home;
    private HashMap<String, String> loginMap;
    private String token;
    private LoginBean.InfoBean.TokenBean tokenBean;
    private int size;
    private String type;
    private String ti;
    private String ban_ben;

    @Override
    protected void initData() {
        mTvTitleHome = findViewById(R.id.tv_title_home);
        con_home = findViewById(R.id.con_home);
        mTvSizeHome = findViewById(R.id.tv_size_home);
        back = findViewById(R.id.back_home);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        int i = 0;
        if (i == 0) {
            initView();
            i = 1;
        }
    }

    @Override
    protected int createLayoutId() {
        return R.layout.activity_home;
    }

    private void initView() {
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        String size = intent.getStringExtra("size");
        String zuo = intent.getStringExtra("zuo");
        ti = intent.getStringExtra("ti2");
        ban_ben = intent.getStringExtra("ban2");
//        Log.e(TAG, "initView: " + type);
        mTvTitleHome.setText(type);

        mTvSizeHome.setText(zuo + "/" + size);

        initLogin();
    }


    private void initLogin() {
        loginMap = new HashMap<>();
        loginMap.put("name", "16619727092");
        loginMap.put("pass", "525952522");
        new Retrofit.Builder()
                .baseUrl(ApiService.baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(ApiService.class)
                .getLoginList(loginMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResourceSubscriber<LoginBean>() {
                    @Override
                    public void onNext(LoginBean loginBean) {
                        tokenBean = loginBean.getInfo().getToken();
                        token = "Bearer " + tokenBean.getAccess_token();
//                        Log.e(TAG, "onNext: 1111111" + HomeActivity.this.token);
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {
                        token = "Bearer " + tokenBean.getAccess_token();
//                        Log.e(TAG, "onNext: 222222" + token);
                        con_home.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(HomeActivity.this, "你单击了" + type, Toast.LENGTH_SHORT).show();
                                initLogin();

                                Intent intent1 = new Intent(HomeActivity.this, SheetActivity.class);
                                intent1.putExtra("token", token);
                                intent1.putExtra("ti_id", ti);
                                intent1.putExtra("ban_ben", ban_ben);
                                startActivity(intent1);
                            }
                        });
                    }
                });
    }

    /*@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            if (isInterception()) {
                if (backListener != null) {
                    backListener.onbackForward();
                    return false;
                }
            }
        }
        return super.onKeyDown(keyCode, event);

    }*/
}